package com.kaibes.module.user.safe;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.core.base.util.StringUtils;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/users/safes")
public class UserSafeApi extends ResponseApi<UserSafe, UserSafeService> {
    
    @GetMapping("/{userId:\\d+}")
    @ApiLink("获取安全信息")
    public ResponseEntity<UserSafe> put(@PathVariable Integer userId) {
        return ResponseFactory.get(service.getByUserId(userId), "获取安全信息成功", "获取用户修改安全信息失败");
    }
    
    @PutMapping("/{userId:\\d+}")
    @ApiLink("修改安全信息")
    public ResponseEntity<Object> put(@PathVariable Integer userId, @RequestBody @Validated UserSafeUpdate data) {
        if (StringUtils.isBlank(data.getQuestion())) {
            data.setQuestion(null);
        }
        if (StringUtils.isBlank(data.getAnswer())) {
            data.setAnswer(null);
        }
        return ResponseFactory.put(service.updateByUserId(data, userId), "用户修改安全信息成功", "用户修改安全信息失败");
    }
    
    @PutMapping("/self")
    @ApiLink(value = "修改安全信息", validated = false)
    public ResponseEntity<Object> putSelf(@RequestAttribute Integer userId, @RequestBody @Validated UserSafeUpdate data) {
        return this.put(userId, data);
    }
    
}
